package com.laot.searchManage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laot.searchManage.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.laot.seacher_master.model.common.BaseResponse;
import org.laot.seacher_master.model.common.ErrorCode;
import org.laot.seacher_master.model.common.PageRequest;
import org.laot.seacher_master.model.common.ResultUtils;
import org.laot.seacher_master.model.entity.User;
import org.laot.seacher_master.model.exception.BusinessException;
import org.laot.searchManage.dto.DataSourceSignSaveDto;
import org.laot.searchManage.entry.DataSourceSign;
import org.laot.searchManage.vo.DataSourceSignPageVo;
import org.laot.searchManage.vo.DataSourceSignVo;
import org.laot.searchManage.service.DataSourceSignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据源登记接口
 * @author laoT
 * @date 2024/1/22
 */
@RestController
@RequestMapping("/dataSourceSign")
@Api(tags = "数据源登记接口")
@Slf4j
public class DataSourceSignController {

    @Resource
    private DataSourceSignService dataSourceSignService;
    @Resource
    private UserUtils userUtils;

    @GetMapping("/listAll")
    @ApiOperation("查询所有")
    public BaseResponse<List<DataSourceSignVo>> listAll(){
        List<DataSourceSignVo> dataSourceSignVoList =  dataSourceSignService.listAll();
        return ResultUtils.success(dataSourceSignVoList);
    }

    @PostMapping("/page")
    @ApiOperation("页面查询")
    public BaseResponse<Page<DataSourceSignPageVo>> page(@RequestBody PageRequest pageRequest){
        Page<DataSourceSignPageVo> page = dataSourceSignService.pageInfo(pageRequest);
        return ResultUtils.success(page);
    }

    @GetMapping("/getDetail")
    @ApiOperation("根据id查询详情")
    public BaseResponse<DataSourceSign> getDetail(Integer id){
        if(id == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        DataSourceSign dataSourceSign = dataSourceSignService.getById(id);
        if(dataSourceSign == null) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(dataSourceSign);
    }

    @PostMapping("/remove")
    @ApiOperation("删除")
    public BaseResponse<String> remove(@RequestBody List<Integer> ids){
        if(CollectionUtils.isEmpty(ids)) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        //查询所有id对应的数据登记
        List<DataSourceSign> dataSourceSigns = dataSourceSignService.listByIds(ids);
        List<Integer> newIds = dataSourceSigns.stream().map(DataSourceSign::getId).collect(Collectors.toList());
        //执行删除
        dataSourceSignService.removeBatchByIds(newIds);
        return ResultUtils.success("删除成功！");
    }

    @PostMapping("/sava")
    @ApiOperation("新增数据源登记")
    public BaseResponse<String> save(@RequestBody DataSourceSignSaveDto dto, HttpServletRequest request){
        User user = userUtils.getLoginUser(request);
        dataSourceSignService.saveInfo(dto, user.getUserName());
        return ResultUtils.success("新增成功！");
    }
    @PostMapping("/updateInfo")
    @ApiOperation("修改数据源登记")
    public BaseResponse<String> updateInfo(@RequestBody DataSourceSignSaveDto dto, HttpServletRequest request){
        User user = userUtils.getLoginUser(request);
        dataSourceSignService.updateInfo(dto,user.getUserName());
        return ResultUtils.success("修改成功！");
    }

    @GetMapping("/updateStatus")
    @ApiOperation("修改开启状态")
    public BaseResponse<String> updateStatus(Integer id, Integer status){
        dataSourceSignService.updateStatus(id,status);
        return ResultUtils.success("修改成功！");
    }




}
